﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using NNS.Framework;
using QLTB.BusinessObjects;

namespace QLTB.LichSu.UI
{
    public partial class frmLichSu : DevExpress.XtraEditors.XtraForm
    {
        public frmLichSu()
        {
            InitializeComponent();
        }

        private void frmLichSu_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsLichSu.TBL_lichsu' table. You can move, or remove it, as needed.
            Nap();
        }

        public void Nap()
        {
            Nap(_thietbi.ID);
        }

        public void Nap(int id)
        {
            try
            {
                tBL_lichsuTableAdapter.Connection.ConnectionString = SqlHelper.ConnectionString;
                this.tBL_lichsuTableAdapter.FillBy(this.dsLichSu.TBL_lichsu, id);
            }
            catch (Exception ex)
            {

                MyBox.ShowWarning(ex.Message, "Quản lý thiết bị");
            }
            
        }

        private TBL_THIETBI _thietbi;
        public TBL_THIETBI ThietBi
        {
            set
            {
                _thietbi = value;
                Nap(_thietbi.ID);
            }
        }

        public void Save()
        {
            try
            {
                tBL_lichsuTableAdapter.Connection.ConnectionString = SqlHelper.ConnectionString;
                tBL_lichsuTableAdapter.Adapter.RowUpdating += new System.Data.SqlClient.SqlRowUpdatingEventHandler(Adapter_RowUpdating);
                tBL_lichsuTableAdapter.Adapter.RowUpdated += new System.Data.SqlClient.SqlRowUpdatedEventHandler(Adapter_RowUpdated);
                this.tBL_lichsuTableAdapter.Update(this.dsLichSu.TBL_lichsu);
            }
            catch (Exception ex)
            {
                MyBox.ShowWarning(ex.Message, "Quản lý thiết bị");
            }
            
        }

        void Adapter_RowUpdated(object sender, System.Data.SqlClient.SqlRowUpdatedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void Adapter_RowUpdating(object sender, System.Data.SqlClient.SqlRowUpdatingEventArgs e)
        {
            //throw new NotImplementedException();
            if (e.Row.RowState!=DataRowState.Deleted)
            {
                Entity.dsLichSu.TBL_lichsuDataTable dt = dsLichSu.TBL_lichsu;
                if (e.Row[dt.ngayColumn]==DBNull.Value)
                {
                    MyBox.ShowWarning("Vui lòng chọn ngày!", "Quản lý lịch sử thiết bị");
                    e.Status = UpdateStatus.SkipAllRemainingRows;
                    return;
                }
            }
            if (e.Status==UpdateStatus.ErrorsOccurred)
            {
                
            }
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Delete)
            {
                Deleted();
            }

            ProcessDialogKey(e.KeyCode);
        }

        private void Deleted()
        {
            if (MyBox.ShowQuestion("Bạn có chắc chắn muốn xóa không?", "Quản lý thiết bị") == DialogResult.No) return;

            if (gbList.SelectedRowsCount>0)
            {
                gbList.DeleteSelectedRows();
            }
            else
            {
                gbList.DeleteRow(gbList.FocusedRowHandle);
            }
        }

        private void gbList_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            Save();
            Nap();
        }

        public enum Column
        {
            None,
            MaTB,
            Lock
        }

        Column m_Column = new Column();

        private void gbList_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            gbList.ClearColumnErrors();

            // gbList.UpdateCurrentRow();
            if (m_Column == Column.Lock) return;
            if (m_Column == Column.None)
            {
                if (e.Column == colngay | e.Column == colnoidung)
                {
                    m_Column = Column.MaTB;
                }
            }

            switch (m_Column)
            {
                case Column.None:
                    return;
                case Column.MaTB:
                    m_Column = Column.Lock;
                    gbList.SetRowCellValue(e.RowHandle,colidthietbi, _thietbi.ID);
                    //gbList.SetRowCellValue(e.RowHandle, "idloaiLK",(short)Style);
                    //gbList.UpdateCurrentRow();
                    m_Column = Column.None;
                    return;
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                //case Keys.Alt | Keys.D:
                //    //MessageBox.Show("You are stupid");
                //    return true;

                //// some more cases...  

                //case Keys.F1:
                //    //ucToolBar_HelpClick(ucBar);
                //    F11Execute();
                //    return true;
                //case Keys.F3:
                //    F3Execute();
                //    return true;
                //case Keys.F4:
                //    F4Execute();
                //    return true;
                //case Keys.F5:
                //    F5Execute();
                //    return true;
                //case Keys.F6:
                //    F9Execute();
                //    return true;
                //case Keys.F7:
                //    F7Execute();
                //    return true;
                //case Keys.F8:
                //    F8Execute();
                //    return true;
                //case Keys.F9:
                //    F9Execute();
                //    return true;
                //case Keys.F11:
                //    F11Execute();
                //    return true;
                //case Keys.F12:
                //    F12Execute();
                //    return true;

                case Keys.Escape:
                    Close();
                    EscapeExecute();
                    return true;

                case Keys.Alt | Keys.X:
                    Close();
                    return true;

                case Keys.F10:
                    F10Execute();
                    Close();
                    return true;
            }

            return false;
        }

        void F10Execute()
        {


        }

        void EscapeExecute()
        {


        }
    }
}